﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using Model.Entities;

namespace Model
{
    public class ArtistProvider : DataAccess
    {
        protected ArtistDetails GetArtistFromReader(IDataReader reader)
        {
            DateTime date = GetDate(reader["FoundationDate"]);
            var artist = new ArtistDetails((int) reader["ArtistID"], reader["ArtistName"].ToString(),
                                           reader["Description"].ToString(), reader["Photo"].ToString(), 
                                           date);
            return artist;
        }

        public ArtistDetails GetEmptyArtist()
        {
            var emptyArtist = new ArtistDetails
                                  {
                                      ArtistId = 0,
                                      ArtistName = "Empty",
                                      Description = "Empty",
                                      Photo = "Empty"
                                  };
            return emptyArtist;
        }

        public List<ArtistDetails> GetArtists()
        {
            var con = new SqlConnection(ConnectionString);
            var cmd = new SqlCommand("GetArtists", con) {CommandType = CommandType.StoredProcedure};
            var artists = new List<ArtistDetails>();
            
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader != null)
            {
                while (reader.Read())
                {
                    artists.Add(GetArtistFromReader(reader));
                }
                reader.Close();
            }
            con.Close();
            
            return artists;
        }

        public int InsertArtist(ArtistDetails art)
        {
            var con = new SqlConnection(ConnectionString);
            var cmd = new SqlCommand("AddArtist", con) { CommandType = CommandType.StoredProcedure };
            if (string.IsNullOrEmpty(art.ArtistName))
                art.ArtistName = "";
            cmd.Parameters.Add("@ArtistName", SqlDbType.NVarChar).Value = art.ArtistName;
            cmd.Parameters.Add("@Description", SqlDbType.NVarChar).Value = art.Description;
            if (string.IsNullOrEmpty(art.Photo))
                art.Photo = "noimage.gif";
            cmd.Parameters.Add("@Photo", SqlDbType.NVarChar).Value = art.Photo;
            cmd.Parameters.Add("@FoundationDate", SqlDbType.DateTime).Value = art.FoundationDate;
            cmd.Parameters.Add("@ArtistID", SqlDbType.Int).Direction = ParameterDirection.Output;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            return (int)cmd.Parameters["@ArtistID"].Value;
        }

        public void UpdateArtist(ArtistDetails art)
        {
            UpdateArtist(art.ArtistId, art.ArtistName, art.Description, art.Photo, (DateTime) art.FoundationDate);
        }

        public void UpdateArtist(int artId, string artName, string descr, string photo, DateTime foundationDate)
        {
            var con = new SqlConnection(ConnectionString);
            var cmd = new SqlCommand("UpdateArtist", con) { CommandType = CommandType.StoredProcedure };

            cmd.Parameters.Add("@ArtistName", SqlDbType.NVarChar).Value = artName;
            cmd.Parameters.Add("@Description", SqlDbType.NVarChar).Value = descr;
            if (string.IsNullOrEmpty(photo))
                photo = "noimage.gif";
            cmd.Parameters.Add("@Photo", SqlDbType.NVarChar).Value = photo;
            cmd.Parameters.Add("@FoundationDate", SqlDbType.DateTime).Value = foundationDate;
            cmd.Parameters.Add("@ArtistID", SqlDbType.Int).Value = artId;
           
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }

        public void DeleteArtist(ArtistDetails art)
        {
            var con = new SqlConnection(ConnectionString);
            var cmd = new SqlCommand("DeleteArtist", con) { CommandType = CommandType.StoredProcedure };

            cmd.Parameters.Add("@ArtistID", SqlDbType.Int).Value = art.ArtistId;
            
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }

        public List<ArtistDetails> GetArtistsByName(string artName)
        {
            if (string.IsNullOrEmpty(artName))
                return GetArtists();
            var con = new SqlConnection(ConnectionString);
            var cmd = new SqlCommand("SearchArtistByName", con) { CommandType = CommandType.StoredProcedure };
            cmd.Parameters.Add("@ArtistName", SqlDbType.NVarChar).Value = artName;
            var artists = new List<ArtistDetails>();
           
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader != null)
            {
                while (reader.Read())
                {
                    artists.Add(GetArtistFromReader(reader));
                }
                reader.Close();
            }
            con.Close();
            return artists;
        }

        public ArtistDetails GetArtistById(int artId)
        {
            var con = new SqlConnection(ConnectionString);
            var cmd = new SqlCommand("SearchArtistById", con) { CommandType = CommandType.StoredProcedure };
            cmd.Parameters.Add("@ArtistId", SqlDbType.Int).Value = artId;
            var artist = new ArtistDetails();

            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader != null)
            {
                while (reader.Read())
                {
                    artist = GetArtistFromReader(reader);
                }
                reader.Close();
            }
            con.Close();
            return artist;
        }
    }
}
